import { parse } from 'url';
import webpath from '@/utils/webpath';
// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        id: `李海波 id ${i}`,
        user_id: `李海波 user_id ${i}`,
        province_code: `李海波 province_code ${i}`,
        province_name: `李海波 province_name ${i}`,
        city_code: `李海波 city_code ${i}`,
        city_name: `李海波 city_name ${i}`,
        county_code: `李海波 county_code ${i}`,
        county_name: `李海波 county_name ${i}`,
        details_address: `李海波 details_address ${i}`,
        is_defalut: `李海波 is_defalut ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}


let optionsData = [
  {
    value: '河北省',
    label: '河北省',
    children: [
      {
        value: '沧州市',
        label: '沧州市',
        children: [
          {
            value: '海兴县',
            label: '海兴县',
          },
        ],
      },
    ],
  },
  {
    value: '山东省',
    label: '山东省',
    children: [
      {
        value: '青岛市',
        label: '青岛市',
        children: [
          {
            value: '崂山区',
            label: '崂山区',
          },
        ],
      },
    ],
  },
];

function getProvincesCitiesCountie (req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = optionsData;

    return res.json(dataSource);
}

function getProUserGeographic(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}



function deleteByPrimaryKeyForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProUserGeographic(req, res, u);
}


function selectByPrimaryKeyForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}



function insertForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        id: `李海波 id ${i}`,
        user_id: `李海波 user_id ${i}`,
        province_code: `李海波 province_code ${i}`,
        province_name: `李海波 province_name ${i}`,
        city_code: `李海波 city_code ${i}`,
        city_name: `李海波 city_name ${i}`,
        county_code: `李海波 county_code ${i}`,
        county_name: `李海波 county_name ${i}`,
        details_address: `李海波 details_address ${i}`,
        is_defalut: `李海波 is_defalut ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getProUserGeographic(req, res, u);
}




function updateForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getProUserGeographic(req, res, u);
}


function deleteByMapForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}



function updateByMapForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}

function getOneForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProUserGeographic(req, res, u);
}

function getAllForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProUserGeographic(req, res, u);
}

function queryPageForProUserGeographic(req, res, u, b) {


    return getProUserGeographic(req, res, u);
}

function countForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}


function insertBatchForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}



function deleteBatchForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}


function updateBatchForProUserGeographic(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProUserGeographic(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/prousergeographic/delete': deleteByPrimaryKeyForProUserGeographic,
    'GET /${webpath}/web/api/service/prousergeographic/get': selectByPrimaryKeyForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/insert': insertForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/update': updateForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/deleteByMap': deleteByMapForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/updateByMap': updateByMapForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/getOne': getOneForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/getAll': getAllForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/queryPage': queryPageForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/count': countForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/insertBatch': insertBatchForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/deleteBatch': deleteBatchForProUserGeographic,
    'POST /${webpath}/web/api/service/prousergeographic/updateBatch': updateBatchForProUserGeographic,
    
    
    
    'POST /${webpath}/web/api/service/prousergeographic/getProvincesCitiesCountie': getProvincesCitiesCountie,
    
    
};
